package com.halachawiki.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.halachawiki.shared.SharedUtil;


public class MultipleStringInput extends FlexTable {

	private List<String> values = new ArrayList<String>();
	private Label debugTotal = new Label();
	public boolean debug = false;
	public MultipleStringInput(String [] initialValues) {
		if (initialValues != null) {
			for (String s : initialValues)
				values.add(s);
		} else
			values.add("");
		if (debug)
			debugUpdateLabelLoop();
		draw();
	}
	
	public List<String> getValue() {
		List<String> result = new ArrayList<String>();
		for (String s : values)
			if (!s.isEmpty())
				result.add(s);
		return result;
	}
	
	public void setValue(List<String> newValues) {
		if (newValues == null || newValues.size()==0) {
			values = new ArrayList<String>();
			values.add("");
		} else
			values = new ArrayList<String>(newValues);
		draw();
	}
		
	private void debugUpdateLabelLoop() {
		Timer t = new Timer() {
			@Override
			public void run() {
				debugTotal.setText(SharedUtil.join(getValue(),"|"));
			} 
		};
		t.scheduleRepeating(500);
	}
	
	private void draw() {
		clear();
		final FlexTable table = this;
		final Button addButton = new Button();
		addButton.setText("+");
		addButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				table.remove(addButton);
				values.add("");
				if (values.size() > 1)
					drawRow(addButton, values.size() - 2, false);
				drawRow(addButton, values.size()-1, true);
			}
		});
		for (int i = 0; i <  values.size(); i++) {
			drawRow(addButton, i, false);
		}
		updateFooter(addButton);
	}

	private void drawRow(final Button addButton, final int row, boolean setFocus) {
		final TextBox tb = new TextBox();
		this.setWidget(row, 0, tb);
		tb.setText(values.get(row));
		tb.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				values.set(row, tb.getText());
				updateFooter(addButton);
			} 
		});
		
		if (values.size()>1) {
			final Button removeButton = new Button();
			removeButton.setText("-");
			setWidget(row,1,removeButton);
			removeButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					values.remove(row);
					draw();
				} 
			});
		}
		if (row > 0) {
			final Button upButton = new Button();
			upButton.setText("^");
			setWidget(row,2,upButton);
			upButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					String s = values.get(row);
					values.set(row, values.get(row-1));
					values.set(row-1, s);
					drawRow(addButton, row-1, true);
					drawRow(addButton, row, false);
					updateFooter(addButton);
				}
			});
		}
		if (row < values.size()-1) {
			final Button downButton = new Button();
			downButton.setText("V");
			setWidget(row,3,downButton);
			downButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					String s = values.get(row);
					values.set(row, values.get(row+1));
					values.set(row+1, s);
					drawRow(addButton, row+1, true);
					drawRow(addButton, row, false);
					updateFooter(addButton);
				}
			});
		}
		if (setFocus) {
			tb.setFocus(true);
		}
	}
	
	private void updateFooter(Button addButton) {
		if (values.size()>0 && !values.get(values.size()-1).isEmpty())
			setWidget(values.size(), 0, addButton);
		setWidget(values.size()+1, 0, debugTotal);
	}
}
